package com.snap.vseries.dialog;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.snap.vseries.R;
import com.snap.vseries.utils.ToastUtils;


public class NormalCountDialog extends DialogFragment {

    private OnCountListener listener;
    private int count;
    private int maxValue;
    private String attrName;
    private String unitName;

    private TextView attrNameTv;
    private TextView unitNameTv;
    private TextView countEt;

    private TextView num0;
    private TextView num1;
    private TextView num2;
    private TextView num3;
    private TextView num4;
    private TextView num5;
    private TextView num6;
    private TextView num7;
    private TextView num8;
    private TextView num9;
    private ImageView numBack;
    private ImageView confirm;

    public DialogFragment setParam(int count, int maxValue, String attrName, String unitName, OnCountListener listener) {
        this.count = count;
        this.maxValue = maxValue;
        this.attrName = attrName;
        this.unitName = unitName;
        this.listener = listener;
        return this;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(STYLE_NO_TITLE, R.style.DimDialogStyle);
    }

    @Override
    public void onStart() {
        super.onStart();
        WindowManager.LayoutParams layoutParams = getDialog().getWindow().getAttributes();
        layoutParams.width = 310;
        getDialog().getWindow().setAttributes(layoutParams);
        getDialog().setCanceledOnTouchOutside(true);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_normal_count, container, false);

        attrNameTv = v.findViewById(R.id.attr_tv);
        unitNameTv = v.findViewById(R.id.unit_tv);
        countEt = v.findViewById(R.id.count_et);

        num0 = v.findViewById(R.id.num0);
        num1 = v.findViewById(R.id.num1);
        num2 = v.findViewById(R.id.num2);
        num3 = v.findViewById(R.id.num3);
        num4 = v.findViewById(R.id.num4);
        num5 = v.findViewById(R.id.num5);
        num6 = v.findViewById(R.id.num6);
        num7 = v.findViewById(R.id.num7);
        num8 = v.findViewById(R.id.num8);
        num9 = v.findViewById(R.id.num9);
        numBack = v.findViewById(R.id.num_back);
        confirm = v.findViewById(R.id.num_ok);


        View.OnClickListener numListener = v12 -> {
            int target = count;
            switch (v12.getId()) {
                case R.id.num0:
                    target *= 10;
                    break;
                case R.id.num1:
                    target = target * 10 + 1;
                    break;
                case R.id.num2:
                    target = target * 10 + 2;
                    break;
                case R.id.num3:
                    target = target * 10 + 3;
                    break;
                case R.id.num4:
                    target = target * 10 + 4;
                    break;
                case R.id.num5:
                    target = target * 10 + 5;
                    break;
                case R.id.num6:
                    target = target * 10 + 6;
                    break;
                case R.id.num7:
                    target = target * 10 + 7;
                    break;
                case R.id.num8:
                    target = target * 10 + 8;
                    break;
                case R.id.num9:
                    target = target * 10 + 9;
                    break;
                case R.id.num_back:
                    target /= 10;
                    break;
                default:
            }
            if (target > maxValue) return;
            count = target;
            countEt.setText(String.valueOf(count));
        };

        attrNameTv.setText(attrName);
        unitNameTv.setText(unitName);
        countEt.setText(String.valueOf(count));

        num0.setOnClickListener(numListener);
        num1.setOnClickListener(numListener);
        num2.setOnClickListener(numListener);
        num3.setOnClickListener(numListener);
        num4.setOnClickListener(numListener);
        num5.setOnClickListener(numListener);
        num6.setOnClickListener(numListener);
        num7.setOnClickListener(numListener);
        num8.setOnClickListener(numListener);
        num9.setOnClickListener(numListener);
        numBack.setOnClickListener(numListener);

        confirm.setOnClickListener(v13 -> {
            if (count == 0) {
                ToastUtils.get().showText("输入不能为0");
                return;
            }
            listener.onCountChanged(count);
            NormalCountDialog.this.dismiss();
        });
        return v;
    }

    public interface OnCountListener {
        void onCountChanged(int count);
    }
}